package com.example.dynamicdatasource.controller;

import com.example.dynamicdatasource.pojo.Province;
import com.example.dynamicdatasource.service.ProvinceService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 省份信息Controller
 * 处理省份相关的HTTP请求，调用Service层实现业务逻辑
 */
@RestController
@RequestMapping("/api/province")
@RequiredArgsConstructor
public class ProvinceDataController {

    private final ProvinceService provinceService;

    /**
     * 1. 简单参数示例：根据省份编码查询省份信息
     * 请求方式：GET
     * 请求URL：/api/province?code=beijing
     * 核心逻辑：接收前端传入的provinceCode，调用Service方法（Service通过入参切换数据源）
     */
    @GetMapping
    public ResponseEntity<Province> getProvinceByCode(@RequestParam("code") String provinceCode) {
        Province province = provinceService.getProvinceByCode(provinceCode);

        if (province != null) {
            return new ResponseEntity<>(province, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }
}
